package com.example.user.customviewdemo.slideletelistview.dellistview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ListView;
import android.widget.Scroller;

/**
 * Created by user on 2016/5/23.
 */
public class DelListView extends ListView {

    private Scroller mScroller;
    private Scroller mPreScroller;


    public DelListView(Context context) {
        super(context);
    }

    public DelListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mScroller = new Scroller(context,new LinearInterpolator());
        mPreScroller = new Scroller(context,new LinearInterpolator());
    }

    public DelListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private float preX;
    private float startX;
    private View scrollLayout = null;
    private View preScrollLayout = null;
    private int position;

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                preX = event.getX();
                startX = event.getX();
                float y = event.getY();
                if(preScrollLayout != null){
                    int preScrollX = preScrollLayout.getScrollX();
                    mPreScroller.startScroll(preScrollX,0,0-preScrollX,0);
                }
                if(position != INVALID_POSITION) {
                    position = pointToPosition((int) preX, (int) y);

                    DelListViewActivity.DelListViewAdapter.DataHolder dataHolder = (DelListViewActivity.DelListViewAdapter.DataHolder) getItemAtPosition(position);
                    scrollLayout = dataHolder.rootLayout;
                }
                break;
            case MotionEvent.ACTION_MOVE:

                //使用scrollTo（）
                float x = event.getX();
                int dx = (int) (preX - x) + scrollLayout.getScrollX(); //移动距离 + 现在的scrollX
                if(dx >= 300){
                    dx = 300;
                }else if(dx < 0){
                    dx = 0;
                }

                scrollLayout.scrollTo(dx,0);
                preX = x;
                break;
            //使用scrollBy（）存在bug，超过之后会出现晃动
                       /* float scrollX = scrollLayout.getScrollX();
                        if(scrollX > 200){
                            scrollLayout.scrollTo(200,0);
                            return true;
                        }else if(scrollX < 0){
                            scrollLayout.scrollTo(0,0);
                            return true;
                        }
                       float x = event.getX();
                        int dx = (int) (preX - x);
                        scrollLayout.scrollBy(dx,0);
                        preX = x;
                        break;*/
            case MotionEvent.ACTION_UP:

                preScrollLayout = scrollLayout;

                float upX = event.getX();
                int dx1 = (int) (startX - upX);
                int scrollX = scrollLayout.getScrollX();
                int absDx1 = Math.abs(dx1);
                if(dx1>0 && absDx1 >= 60 && scrollLayout != null){
                    //scrollLayout.scrollTo(300,0);
                    mScroller.startScroll(scrollX,0,300-scrollX,0);
                }else if(dx1 < 0 && absDx1 >=60){
                    //scrollLayout.scrollTo(0,0);
                    mScroller.startScroll(scrollX,0,0-scrollX,0);
                }
                break;

        }

        return super.onTouchEvent(event);
    }

    @Override
    public void computeScroll() {
        if(mScroller.computeScrollOffset() && scrollLayout != null){
            scrollLayout.scrollTo(mScroller.getCurrX(),mScroller.getCurrY());
        }
        if(mPreScroller.computeScrollOffset() && preScrollLayout != null){
            preScrollLayout.scrollTo(mPreScroller.getCurrX(),mPreScroller.getCurrY());
        }
        invalidate();
    }
}
